package com.car.loan.project.tool.controller;

import com.car.loan.common.utils.IdUtils;
import com.car.loan.common.utils.file.FileUtils;
import com.car.loan.common.utils.qiniu.QiniuUtil;
import com.car.loan.framework.task.UploadImageTask;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.cd.domain.vo.CreditAlbumVo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author huangding
 * @description
 * @date 2019/10/24 20:46
 */
@RestController
@RequestMapping("/tool/image")
@Api(value = "ocr接口", tags = "工具接口-图片上传")
@Slf4j
public class ImageController {

    @Autowired
    private QiniuUtil qiniuUtil;

    @Autowired
    private UploadImageTask uploadImageTask;

    @PostMapping("/upload")
    @ApiOperation(value = "上传图片基本接口", response = CreditAlbumVo.class)
    public AjaxResult upload(@RequestParam("file") MultipartFile file, String prefix) {
        if (FileUtils.fileNameIsImg(file.getOriginalFilename())) {
            try {
                return AjaxResult
                    .uploadSuccess(qiniuUtil.uploadBase640(file, "image/" + prefix), qiniuUtil);
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                return AjaxResult.error("上传失败，请重试");
            }
        }
        return AjaxResult.error("图片格式错误");
    }

    @GetMapping("/getUpToken")
    public AjaxResult getUpToken() {
        return AjaxResult.success("操作成功", qiniuUtil.getRedisUpToken());
    }

//    @PostMapping("/batchUpload")
//    @ApiOperation(value = "批量上传接口", notes = "直接返回上传链接,可能需要过一段时间才可打开", response = CreditAlbumVo.class, responseContainer = "list")
//    public AjaxResult batchUpload(@RequestParam("files") MultipartFile[] files, String prefix) {
//        if (null != files && files.length > 0) {
//
//            List<CreditAlbumVo> paths = Lists.newArrayList();
//            for (MultipartFile file : files) {
//                if (FileUtils.fileNameIsImg(file.getOriginalFilename())) {
//                    String fileName =
//                        prefix + "/" + IdUtils.simpleUUID() + "." + Objects
//                            .requireNonNull(file.getOriginalFilename()).split("\\.")[1];
//                    try {
//                        String string = uploadImageTask.uploadImage(file.getBytes(), fileName);
//
//                    } catch (IOException e) {
//                        log.error(e.getMessage(), e);
//                    }
//                    paths.add(new CreditAlbumVo().setDbUrl(fileName)
//                        .setUrl(qiniuUtil.getAuthUrl(fileName)));
//                } else {
//                    return AjaxResult.error("图片格式错误");
//                }
//            }
//            return AjaxResult.success(paths);
//        }
//        return AjaxResult.error("图片格式错误");
//    }

    @GetMapping("test")
    @ApiModelProperty("测试图片,url就是图片上传返回的字符串，不需添加前缀")
    public AjaxResult test(String url) {
        return AjaxResult.success(qiniuUtil.getAuthUrl(url));
    }

}
